using System;
using System.Collections.Generic;
using System.Text;

namespace ReinforcementLearning
{
    public class ProbabilityTable
    {
        private int scaleSize;
        private List<Action> actionsTable;

        public ProbabilityTable(List<ProbabilityTableEntry> set)
        {
            this.scaleSize = 1000;
            this.buildTable(set);
        }

        private void buildTable(List<ProbabilityTableEntry> set)
        {
            double somatory = 0;

            foreach (ProbabilityTableEntry probEntry in set)
            {
                somatory += probEntry.probability;
            }

            actionsTable = new List<Action>();

            foreach (ProbabilityTableEntry probEntry in set)
            {
                int times = (int)Math.Round((probEntry.probability/somatory) * this.scaleSize);

                for (int i = 0; i < times; i++)
                {
                    actionsTable.Add(probEntry.action);
                }
            }
        }

        public Action sortAnAction()
        {
            Random random = new Random();
            double randomNumber = Math.Round(random.NextDouble() * this.scaleSize);
            if (randomNumber >= this.actionsTable.Count)
            {
                randomNumber = this.actionsTable.Count - 1;
            }
            Action action = Action.UNDEFINED;
            action = this.actionsTable.ToArray()[(int)randomNumber];
            return action;
        }




    }
}
